

function id(id) {
    return document.getElementById(id);
}

function bg() {
    return chrome.extension.getBackgroundPage();
}

function buildMyAccordion(search_str) {
    var div;
    var outer_div = document.createElement("div");

    if (bg().requests.length == 0) {
        
        div = document.createElement("div");
        div.innerHTML = "---No requests available---<br/>Issue HTTP requests to forge these.";
        outer_div.appendChild(div);
    }
    else {
        var found = false;
        var mehtod_str;
        var param_str;
        var p_arr;
        var font;

        for (i = 0; i < bg().requests.length; i++) {
            if (search_str !== null && search_str != undefined && search_str !== "") {
                if(search_str == "POST" || search_str == "GET") {
                    if (bg().requests[i].method.toUpperCase() != search_str) {
                        continue;
                    }
                }
                else if (bg().requests[i].url.toString().indexOf(search_str) == -1) {
                    continue;
                }
            }
            found = true;

            div = document.createElement("div");
            method_str = bg().requests[i].method.toUpperCase();
            if (method_str == "POST") {
                method_str = '<font color="#B80000">' + method_str + "</font>";
            }
            else {
                method_str = '<font color="#0000FF">' + method_str + "</font>";
            }
            var html_str = method_str;  + ": " + bg().requests[i].url;
            if (bg().requests[i].session_warning) {
                html_str += ": <b><font color='red'>! </font></b>";
            }
            else {
                html_str += ": ";
            }
            html_str += bg().requests[i].url;
            div.innerHTML = html_str;
            div.setAttribute("style", "width:100%;white-space:nowrap;background-color:#E6E6E6;border-width:1px;border-style:solid;border-color:#A4A4A4;");
            div.onmouseover = mouseover_div;
            div.onmouseout = mouseout_div;
            div.onclick = getOpenDivOnClick(i);
            outer_div.appendChild(div);
            div = document.createElement("div");
            div.id = "open_div_" + i;
            div.setAttribute("style", "display:none;"); // ueber onclick auf display:block setzen
            outer_div.appendChild(div);

        }
        if (!found) {
            div = document.createElement("div");
            div.innerHTML = "Searching: " + search_str + "<br/>---No matches found---";
            outer_div.appendChild(div);
        }
    }
    id("accordion").appendChild(outer_div);
}


function mouseover_details() {
    this.style.cursor = "pointer";
    this.style.backgroundColor = "#A9F5D0";
}

function mouseout_details() {
    this.style.cursor = "auto";
    this.style.backgroundColor = "";
}

function mouseover_div() {
    this.style.cursor = "pointer";
    this.style.backgroundColor = "#F6D8CE";
}

function mouseout_div() {
    this.style.cursor = "auto";
    this.style.backgroundColor = "#E6E6E6";
}

function getOpenDivOnClick(i) {
    handler = function () {
        var div = id("open_div_" + i);
        if (div.style.display == "none") {
            // aufklappen
            var param_str = "";
            var p_arr = bg().requests[i].params;
            if (p_arr.length > 0) {
                for (j = 0; j < p_arr.length; j++) {
                    param_str += (encodeURIComponent(p_arr[j].name) + "=" + encodeURIComponent(p_arr[j].value) + "&");
                }
            }
            else {
                param_str += "No Parameters-";
            }
            param_str = param_str.substr(0, param_str.length - 1);
            div.innerHTML = "&nbsp;&nbsp;&rarr;&nbsp;&nbsp;" + param_str;
            div.onmouseover = mouseover_details;
            div.onmouseout = mouseout_details;
            div.onclick = getHandler(i);
            div.style.display = "block";
        }
        else {
            // zuklappen
            div.innerHTML = "";
            div.style.display = "none";
        }
    }
    return handler;
}

function getHandler(i) {
    // handler muss in separater Function erstellt werden, wird der hanlder iteriert erzeugt wird er immer selber ueberschrieben
    handler = function () {
        //alert(JSON.stringify(bg().requests[i]));
        var url_str = chrome.extension.getURL("ForgePanel.html") + "?r=" + encodeURIComponent(JSON.stringify(i));
        chrome.tabs.create({ url: url_str });
    };
    return handler;
}

function setIcon() {
    if (bg().capture_mode > -1) {
        if (bg().blocking_mode) {
            if (bg().warning_found) {
                chrome.browserAction.setIcon({ path: "IconRedWarning.ico" });
            }
            else {
                chrome.browserAction.setIcon({ path: "IconRed.ico" });
            }
        }
        else {
            if (bg().warning_found) {
                chrome.browserAction.setIcon({ path: "IconGreenWarning.ico" });
            }
            else {
                chrome.browserAction.setIcon({ path: "IconGreen.ico" });
            }
        }
    }
    else {
        if (bg().warning_found) {
            chrome.browserAction.setIcon({ path: "IconWarning.ico" });
        }
        else {
            chrome.browserAction.setIcon({ path: "Icon.ico" });
        }
    }
}

id("blocking_mode_button").onclick = function () {
    if (this.checked == true) {
        bg().blocking_mode = true;
        setIcon();
    }
    else {
        bg().blocking_mode = false;
        setIcon();
    }
}

id("radio0").onclick = function () {
    bg().capture_mode = 0; // capture all
    setIcon();
};
id("radio1").onclick = function () {
    bg().capture_mode = 1; // capture only form active tab
    setIcon();
};
id("radio2").onclick = function () {
    bg().capture_mode = -1; // capture nothing
    setIcon();
};

if (bg().capture_mode == 0) {
    id("radio0").checked = "checked";
} else if (bg().capture_mode == 1) {
    id("radio1").checked = "checked";
} else if (bg().capture_mode == -1) {
    id("radio2").checked = "checked";
}

id("filter_button").onclick = function () {
    filter_str = jQuery.trim(id("url_filter").value);
    if (filter_str == "") {
        id("accordion").innerHTML = "";
        buildMyAccordion(null);        
        bg().filter_str = null;
    }
    id("accordion").innerHTML = "";
    buildMyAccordion(filter_str);    
    bg().filter_str = filter_str;
};

id("reset_button").onclick = function () {
    bg().requests = [];
    bg().sessions = {};
    bg().warning_found = false;
    id("accordion").innerHTML = "";
    buildMyAccordion();
    setIcon();
};

id("dump_all_button").onclick = function () {
    open_url_str = "Dump.html?r=ALL";
    var newWin = window.open(open_url_str, "SessionID Menu", "height=800, width=1024,toolbar=no,scrollbars=yes,menubar=no,location=no");
};

id("load_dump_button").onclick = function () {
    open_url_str = "Dump.html?r=LOAD";
    var newWin = window.open(open_url_str, "SessionID Menu", "height=800, width=1024,toolbar=no,scrollbars=yes,menubar=no,location=no");
}

if (bg().filter_str !== null) {
    id("url_filter").value = bg().filter_str;
    buildMyAccordion(bg().filter_str);
}
else {
    buildMyAccordion(null);
}

setIcon();

if (bg().blocking_mode) {
    id("blocking_mode_button").checked = "checked";
}
else {
    id("blocking_mode_button").checked = "";
}


$(document).ready(function () {
    //$("#accordion").accordion();
    $("#radio").buttonset();
    $("#reset_button").button();
    $("#filter_button").button();
    $("#add_sessionid_name").button();
    $("#dump_all_button").button();
    $("#load_dump_button").button();
    $("#blocking_mode_button").button();
});